package util;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import net.sf.json.JSONObject;
import server.SettingServer;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;


public class PlayNoticeAudio extends PlayAudio implements Runnable {
    private static PlayNoticeAudio playNoticeAudio = null;

    private PlayNoticeAudio() {
    }

    public static PlayNoticeAudio newInstance() {
        if (playNoticeAudio == null) {
            playNoticeAudio = new PlayNoticeAudio();
        }
        return playNoticeAudio;
    }

    public static PlayNoticeAudio getPlayNoticeAudio() {
        return playNoticeAudio;
    }

    @Override
    public void run() {
        try {
            SettingServer settingServer = new SettingServer();
            settingServer.open();
            JSONObject gameInfo;
            boolean startAudio;
            boolean timeIsUpAudio;
            boolean stopAudio;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            long startTime;
            long stopTime;
            long nowTime;
            long timeIsUp;
            String[] temp;
            while (true) {
                gameInfo = settingServer.readSettingObject("game");
                startAudio = gameInfo.getBoolean("start_audio");
                timeIsUpAudio = gameInfo.getBoolean("time_is_up_audio");
                stopAudio = gameInfo.getBoolean("stop_audio");

                temp = gameInfo.getString("time_is_up").split(":");
                timeIsUp = (Integer.parseInt(temp[0]) * 360 + Integer.parseInt(temp[1]) * 60) * 1000;

                stopTime = simpleDateFormat.parse(gameInfo.getString("stop")).getTime();
                startTime = simpleDateFormat.parse(gameInfo.getString("start")).getTime();

                nowTime = new Date().getTime() + 1000;

                if (!startAudio && nowTime >= startTime && (nowTime - startTime) <= 5000) {
                    playAudio(gameInfo, "start_audio", "Start-Audio");
                }
                if (!timeIsUpAudio && (stopTime - startTime) >= timeIsUp && (stopTime - nowTime) <= timeIsUp) {
                    playAudio(gameInfo, "time_is_up_audio", "Time-Is-Up-Audio");
                }
                if (!stopAudio && nowTime >= stopTime && (nowTime - stopTime) <= 5000) {
                    playAudio(gameInfo, "stop_audio", "Stop-Audio");
                }
                Thread.sleep(1000);
            }
        } catch (JavaLayerException | FileNotFoundException | InterruptedException | ParseException e) {
            e.printStackTrace();
        }
    }

    public void playAudio(JSONObject gameInfo, String gameKey, String audioKey) throws JavaLayerException, FileNotFoundException {
        SettingServer settingServer = new SettingServer();
        settingServer.open();
        gameInfo.replace(gameKey, true);
        settingServer.recordSettingObject("game", gameInfo);
        ReadConfig readConfig = new ReadConfig();
        LinkedHashMap<String, String> audioConfig = readConfig.readConfig("audio.json");
        if (audioConfig.containsKey(audioKey)) {
            setAudioFile(audioConfig.get(audioKey));
            if (audioFile != null && audioFile.exists()) {
                BufferedInputStream audio = new BufferedInputStream(new FileInputStream(audioFile));
                player = new Player(audio);
                player.play();
            }
        }
        settingServer.close();
    }
}
